package shuati.meituan;

import java.util.*;

public class Main {
    
    public static void main(String[] args) {
        int[] nums = {1, 2, 2};
        int result = sumOfModes(nums);
        System.out.println("Sum of modes: " + result);
    }
    
    public static int sumOfModes(int[] nums) {
        int sum = 0;
        int n = nums.length;
        
        for (int mask = 1; mask < (1 << n); mask++) {
            List<Integer> subset = new ArrayList<>();
            
            // Construct subset based on bitmask
            for (int i = 0; i < n; i++) {
                if ((mask & (1 << i)) != 0) {
                    subset.add(nums[i]);
                }
            }
            
            // Calculate mode(s) in current subset
            int minMode = calculateMinMode(subset);
            sum += minMode;
        }
        
        return sum;
    }
    
    // Function to calculate the minimum mode in a given subset
    public static int calculateMinMode(List<Integer> subset) {
        Map<Integer, Integer> freq = new HashMap<>();
        
        // Count frequencies of numbers in subset
        for (int num : subset) {
            freq.put(num, freq.getOrDefault(num, 0) + 1);
        }
        
        // Find minimum mode in subset
        int minMode = Integer.MAX_VALUE;
        for (int key : freq.keySet()) {
            minMode = Math.min(minMode, freq.get(key));
        }
        
        return minMode;
    }
}
